package main

import (
	"encoding/base64"
	"flag"
	"fmt"
	"gov-auto-ui/src/util"
	"io/ioutil"
	"os"
	"path/filepath"
	"runtime"
)

var pth string

func main() {
	flag.StringVar(&pth, "d", "", "ui dir")
	flag.Parse()
	if pth == "" {
		pth = "./web/dist"
	}
	err := gengo()
	if err != nil {
		println("bdzip err:" + err.Error())
	}
	//deCode64()
}

func deCode64() {
	//ddd, _ := base64.StdEncoding.DecodeString(comm.StaticPkg) //成图片文件并把文件写入到buffer
	//ioutil.WriteFile("./output.zip", ddd, 0666)   //buffer输出到jpg文件中（不做处理，直接写到文件）
}

func gengo() error {
	homeStr:=homePath()
	zipfl := filepath.Join(homeStr, "/001/"+"dist.zip")
	os.RemoveAll(zipfl)
	defer os.RemoveAll(zipfl)
	err := util.Zip(pth, zipfl, true)
	if err != nil {
		return err
	}
	bts, err := ioutil.ReadFile(zipfl)
	if err != nil {
		return err
	}
	cont := base64.StdEncoding.EncodeToString(bts)
	err = ioutil.WriteFile("./web/uis.go",
		[]byte(fmt.Sprintf("package comm\n\nconst StaticPkg = \"%s\"", cont)),
		0644)
	if err != nil {
		return err
	}
	println("ui insert go ok!!!")
	return nil
}

func homePath() string {
	home := ""
	if runtime.GOOS == "windows" {
		home = os.Getenv("USERPROFILE")
	} else {
		home = os.Getenv("HOME")
	}
	return home
}

